package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/admin/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

//    分页查询
    @GetMapping("/page")
    public Result<PageResult>  page(SetmealPageQueryDTO setmealpage){
        log.info("分页查询,参数：{}",setmealpage.toString());

        PageResult pageinfo = setmealService.list(setmealpage);

        return Result.success(pageinfo);
    }

//    修改套餐
    @PutMapping
    public Result updateDish(@RequestBody SetmealDTO setmealDTO){

        setmealService.updateDish(setmealDTO);

        return Result.success();
    }

//    套餐起售、停售
    @PostMapping("/status/{status}")
    public Result updateStatus(Long id,@PathVariable Integer status){

        try {
            setmealService.updateStatus(id, status);
        }catch (Exception e){
            return Result.error(e.toString());
        }

        return Result.success();
    }

//    批量删除套餐
    @DeleteMapping
    public Result removeById(Long[] ids){
        setmealService.removeById(ids);
        return Result.success();
    }



    //    新增套餐
    @PostMapping
    public Result save(@RequestBody SetmealDTO setmealDTO){
        setmealService.save(setmealDTO);
        return Result.success();
    }


//    根据id查询套餐
    @GetMapping("/{id}")
    public Result<SetmealVO> selectById(@PathVariable Long id){
        SetmealVO setmealVO = setmealService.selectById(id);

        if (setmealVO != null){
            return Result.success(setmealVO);
        }
        return Result.error("失败");
    }

}
